package org.cttic.visualization.visualization.z_service.first;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.cttic.visualization.visualization.util.ResponseObject;
import org.cttic.visualization.visualization.z_service.AssistChargingCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;


/**
 *
 */
@Service
public class AssistChargingCollectServerImpl implements AssistChargingCollectService {

    private static double random = 14.0d;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public ResponseObject collectStatistics(JSONObject json) {

        JSONObject response = new JSONObject();

        String provinceId = json.getStr("provinceId");

        String roadSectionId = json.getStr("roadSectionId");

        String pointType = json.getStr("pointType");

        String queryType = json.getStr("queryType");

        String queryValue1 = json.getStr("queryValue1");

        String queryValue2 = json.getStr("queryValue2");


        //求计费总数
        String countSQl = " SELECT SUM(serviceNums) FROM serviceCount";
        String sql = "SELECT pointType,sum(serviceNums) c1 FROM serviceCount where provinceId <> '99' group by pointType  ";
        if(StrUtil.isNotEmpty(provinceId)){
            String[] split = provinceId.split("\\|");

            String condition = "";
            for (String s : split) {
                condition += "'" +s+"',";
            }
            condition = condition.substring(0,condition.length()-1);

            countSQl += " where provinceId in ("+condition+")";
            sql = "SELECT pointType,sum(serviceNums) c1 FROM serviceCount where  provinceId in ("+condition+")   group by pointType " ;
        }


        int serTotalNums = jdbcTemplate.queryForObject(countSQl, Integer.class).intValue();


        java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
        nf.setGroupingUsed(false);

        /**
         * ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
         * double random = threadLocalRandom.nextDouble(13D, 20D);
         * double d = getD(random);
         */
        random += (0.001*0.001);

        if(random > 20D){
            random = 15D;
        }


        double serTotalFees = serTotalNums * random;




        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);


        List<JSONObject> pointTypeArray = new ArrayList<>();


        for (Map<String, Object> map : maps) {
            String pointType1 = map.get("pointType").toString();
            int serviceNums = Integer.parseInt(map.get("c1").toString());

            double serviceFees = serviceNums * random;

            JSONObject object = JSONUtil.createObj();


            object.put("pointType", pointType1)
                .put("serviceNums", serviceNums + "")
                .put("serviceFees", nf.format(getD(serviceFees)));


            double serNumsRatio = (serviceNums * 100.0) / serTotalNums;
            object.put("serNumsRatio", getD(serNumsRatio) + "");

            double serFeesRatio = (serviceFees * 100.0) / serTotalFees;
            object.put("serFeesRatio", getD(serFeesRatio) + "");

            pointTypeArray.add(object);
        }

        response.put("serTotalNums", serTotalNums + "")
            .put("serTotalFees", nf.format(serTotalFees))
            .put("pointTypeArray", pointTypeArray);


        return ResponseObject.success(response);
    }


    /**
     * 获取小数点后两位
     *
     * @param d
     * @return
     */
    private double getD(double d) {
        BigDecimal bg = new BigDecimal(d);

        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        return f1;
    }
}
